package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.SelectDiseaseRVAdapter;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.InfantSelBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

import static android.widget.Toast.makeText;

/**
 * Created by liming on 2017/10/19.
 * 详细症状选择
 */

public class DiseaseDetailActivity extends BaseActivity implements SelectDiseaseRVAdapter.OnItemClickListener {

    @Bind(R.id.rv_select)
    RecyclerView mRecyclerView;

    private TextView mTv_submit;
    private String age;//年龄
    private String sex;//性别
    private String cycle;//城市
    private String symptoms;
    private SelectDiseaseRVAdapter mAdapter;
    private ArrayList<InfantSelBean.DataBean> mData;//集合数据

    /*set集合 保存选择的数据*/
    HashSet<String> set = new HashSet<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_disease_detail);
        ButterKnife.bind(this);
        age = getIntent().getStringExtra("age");
        sex = getIntent().getStringExtra("sex");
        cycle = getIntent().getStringExtra("cycle");
        symptoms = getIntent().getStringExtra("symptoms");
        /*recyclerview 适配*/
        if (mAdapter == null) {
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            mData = new ArrayList();
            mAdapter = new SelectDiseaseRVAdapter(mData);
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setNestedScrollingEnabled(false);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        mAdapter.setOnItemClickListener(this);
        getServerData();
    }

    /*获取服务器数据*/
    private void getServerData() {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<String, String>() {
            {
                put("age", age);
                put("sex", sex);
                put("cycle", cycle);
                put("symptoms", symptoms);
            }
        };
        NetHelper.post(APIServer.INFANT_DETAIL, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                InfantSelBean bean = NetHelper.getBeanForJson(InfantSelBean.class, s);
                if (bean == null) return;
                if(bean.getCode().equals("100")) {
                    mData.clear();
                    mData.addAll(bean.getData());
                    mAdapter.notifyDataSetChanged();
                }else {
                    ToastUtil.show(DiseaseDetailActivity.this, bean.getMsg());
                }
                dismissDialog();
            }
        });
    }

    /**
     * 点击事件
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.submit:
                if (set.size() == 0) {
                    makeText(this, "请选择", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(this, MedicalResultActivity.class);
                intent.putExtra("age", age);
                intent.putExtra("sex", sex);
                intent.putExtra("cycle", cycle);
                intent.putExtra("symptoms", symptoms);
                StringBuilder sb = new StringBuilder();
                for (String s : set) {
                    sb.append(s + ",");
                }
                intent.putExtra("parent", sb.substring(0, sb.length()-1));
                startActivity(intent);
                break;
        }
    }

    /**
     * 条目点击事件
     * @param id
     * @param isChec
     */
    @Override
    public void onItemClick(String id, boolean isChec) {
        if (isChec) {
            set.add(id);
        } else {
            set.remove(id);
        }
    }
}

